package com.example.demo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.config.ConfigBean;
import com.example.demo.entity.FileInfo;
import com.example.demo.entity.UploadAddress;
import com.example.demo.entity.UploadComplete;
import com.example.demo.entity.UploadPrepare;
import com.example.demo.utils.AliyunOss;
import com.example.demo.utils.FileVersionCache;

@Service("WebOfficeEditService")
public class WebOfficeEditService implements EditService {
    @Autowired
    private ConfigBean configBean;

    @Autowired
    private AliyunOss aliyunOss;

    public UploadPrepare UploadPrepare(String fileId) {
        /*
         * 根据文档设置文档校验和算法
         */
        UploadPrepare resp = new UploadPrepare();
        List<String> shaList = new ArrayList<>();
        // 设置对应的文档校验和算法，支持的算法有：sha1、md5、sha256，返回一个即可
        shaList.add("sha1");
        shaList.add("md5");
        shaList.add("sha256");
        resp.setDigestTypes(shaList);

        return resp;
    }

    public UploadAddress.Response uploadAddress(UploadAddress.Request request) {
        UploadAddress.Response uploadAddressResp = new UploadAddress.Response();
        /*
         * 解析request，获取数据做业务逻辑，此处省略，如做文档验签等
         */

        /*
         * 如果是本地服务器实现上传，这里需要返回服务器的uploadUrl和对应参数，参考FileServiceController.uploadFile
         * 如果是第三方oss存储，这里需要返回oss的uploadUrl和对应参数，参考OssStorage接口实现类
         */
        String objectName = UUID.randomUUID().toString();
        objectName = request.getFileId();
        objectName = "test/chenmin/"+ objectName+".docx";
        String url = configBean.getOssURL() + objectName;

        uploadAddressResp.setUrl(url); 
        uploadAddressResp.setMethod("PUT"); // 设置文档url的请求方法，支持PUT

        Map<String, String> extraHeaders =aliyunOss.getHeaders(objectName); // 设置下载文件额外的请求头
        uploadAddressResp.setExtraHeaders(extraHeaders);

        Map<String, String> extraParams = aliyunOss.getParams(); // 设置下载文件额外的请求参数
        uploadAddressResp.setExtraParams(extraParams);

        Map<String, String> sendBackParams =  aliyunOss.getSendBackParams(); // 设置上传完成后，回调通知上传结果的请求参数
        uploadAddressResp.setSendBackParams(sendBackParams);
        return uploadAddressResp;
    }

    public FileInfo uploadComplete(UploadComplete.Request request) {
        FileInfo fileInfo = new FileInfo();
        /*
         * 
         * 设置fileInfo的属性，这里需要根据实际业务逻辑设置fileInfo的属性
         */
        long createTime = System.currentTimeMillis()/1000;
        long modifyTime = System.currentTimeMillis()/1000;
        String fileId = request.getFileId();
        fileInfo.setId(request.getFileId());

        Long srcVersion = FileVersionCache.getInstance().get(fileId);
        // 每次编辑保存，版本号加1,这里需要判断第三方返回的response stats_code是否为200，如果不是则不更新版本号！
        srcVersion ++;
        FileVersionCache.getInstance().put(request.getFileId(), srcVersion);

        fileInfo.setVersion(srcVersion);
        fileInfo.setName("文件名.docx");
        fileInfo.setSize(1024);
        fileInfo.setCreateTime(createTime);
        fileInfo.setModifyTime(modifyTime);
        fileInfo.setCreatorId("1235677");
        fileInfo.setModifierId("456546464");
        return fileInfo;
    }
}
